package com.webkitdemo;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;



public class WebKitDemo extends Activity {
     WebView engine;

     final Activity activity = this;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_kit_demo);
        
        
      
        engine = (WebView) findViewById(R.id.webview);
		//engine.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        //engine.loadDataWithBaseURL("http://google.com", "some_data", "text/html","utf-8", null);
        engine.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        engine.loadUrl("file:///sdcard/PASA_HTML5_HMI/CentralConsole/Integrated/MainScreen.html");
        WebSettings webSettings = engine.getSettings();
        webSettings.setJavaScriptEnabled(true);
        engine.setWebViewClient(new MyWebViewClient());
      
        engine.requestFocus();
        // Set to Full screen
        
        engine.setWebChromeClient(new WebChromeClient() {
      	  public boolean onConsoleMessage(ConsoleMessage cm) {
      	    Log.d("MyApplication", cm.message() + " -- From line "
      	                         + cm.lineNumber() + " of "
      	                         + cm.sourceId() );
      	    return true;
      	  }

      	});
       
        
    }
    private class MyWebViewClient extends WebViewClient {
    	@Override
    	public boolean shouldOverrideUrlLoading(WebView view, String url) {
    		if (Uri.parse(url).equals("file:///sdcard/PASA_HTML5_HMI/CentralConsole/Integrated/MainScreen.html")) {
    			return false;
    		}
			//Otherwise, the link is not for a page on my site, so launch another Activity
    		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    		startActivity(intent);
    		return true;
    	}
    	
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
		
		if (engine != null && (keyCode == KeyEvent.KEYCODE_BACK) && engine.canGoBack()) {
			engine.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown( keyCode, event);
    }
    
    
    @Override
    public void onResume()
    {
    	try {
			//HIDE TOOLBAR
			
			    //REQUIRES ROOT
			    Build.VERSION_CODES vc = new Build.VERSION_CODES();
			    Build.VERSION vr = new Build.VERSION();
			    String ProcID = "79"; //HONEYCOMB AND OLDER

			    //v.RELEASE  //4.0.3
			    if(vr.SDK_INT >= vc.ICE_CREAM_SANDWICH)
			    {
			        ProcID = "42"; //ICS AND NEWER
			    }
			    ProcID = "42";
			    //REQUIRES ROOT
			    Runtime.getRuntime().exec("sh"); 
			    Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","service call activity "+ ProcID +" s16 com.android.systemui"}); //WAS 79
			    proc.waitFor();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
		} 
    	super.onResume();
    }
    
    
    @Override
    public void onStop()
    {
    	
    		try {
    		Runtime.getRuntime().exec("sh"); 
    		Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"});
    		proc.waitFor();
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	} catch (InterruptedException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}

        	super.onStop();
        
    }
    
    @Override
    public void onDestroy()
    {
		try {
		Runtime.getRuntime().exec("sh"); 
		Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"});
		proc.waitFor();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

    	super.onDestroy();
    }
  


}
